package com.zhijiaoyun.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.dao.IHelpCenterDao;
import com.zhijiaoyun.model.HelpCenter;
import com.zhijiaoyun.model.dto.PageData;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.IHelpCenterService;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Service(value = "helpCenterService")
public class HelpCenterServiceImpl implements IHelpCenterService {

	@Resource
	IHelpCenterDao helpCenterDao;
	
	@Override
	public void addHelpCenter(HelpCenter helpCenter) {
		helpCenterDao.insert(helpCenter);
	}

	@Override
	public HelpCenter getHelpCenterByHelpId(Long helpId) {
		return helpCenterDao.selectByPrimaryKey(helpId);
	}

	@Override
	public void modifyHelpCenter(HelpCenter helpCenter) {
		helpCenterDao.updateByPrimaryKey(helpCenter);
	}

	@Override
	public PageList<HelpCenter> pageByParams(Integer pageIndex, String text) {
		int count = helpCenterDao.countByParams(text);
		PageData pageData = new PageData();
		List<HelpCenter> list = new ArrayList<>();
		PageList<HelpCenter> pageList = new PageList<>();
		if (count > 0) {
			int pageIndexInt = pageIndex == null ? 1 : pageIndex;
			pageData = PageData.createPage(count, pageIndexInt, DsConstant.PAGESIZE_DEFAULT);
			list = helpCenterDao.pageByParams(pageData.getBeginIndex(), pageData.getPageSize(), text);
			pageList = new PageList<>(list, pageData);
		}
		return pageList;
	}

	@Override
	public void deleteHelpCenter(HelpCenter helpCenter) {
		helpCenterDao.deleteByPrimaryKey(helpCenter.getHelpId());
	}

}
